#include "creep.h"
#include <iostream>
#include <SDL/SDL_gfxPrimitives.h>

extern SDL_Surface * screen;

using namespace std;

void CCreep::Do() {
    if (getTicks() >= GetOpDelay()) {
        for (int i = 0; i < getTicks()/GetOpDelay(); i++) {
            MoveCreep();
        }
        setTime(getTime() + GetOpDelay()*(getTicks()/GetOpDelay()));
    }
}

CCreep::CCreep() {

}

CCreep::CCreep(int tx, int ty, CScene *scene, int shp) {
    SetX(tx);
    SetY(ty);
    SetW(6);
    SetH(6);
    SetOpDelay(20);
    setTime(getTime() + GetOpDelay()*(getTicks()/GetOpDelay()));
    if (getTime() < 0) setTime(0);
    smer = SMER_VLAVO;
    waypoints = level1;    
    gamescene = scene;
    hp = shp;
}

void CCreep::Draw() {
    int ax = GetX();
    int ay = GetY();
    int bx = GetW()/2;
    int by = GetH()/2;
    rectangleRGBA(screen,ax-bx,ay-by,ax+bx,ay+by,0xFF,0xFF,0xFF,0xFF);
}

bool CCreep::isCreep() {
    return true;
}

void CCreep::SetVX(int tx) {
    vx = tx;
}

void CCreep::SetVY(int ty) {
    vy = ty;
}

void CCreep::SetV(int tx, int ty) {
    vx = tx;
    vy = ty;
}

int CCreep::GetVX() {
    return vx;
}

int CCreep::GetVY() {
    return vy;
}

void CCreep::MoveCreep() {
    Move(sdx[smer],sdy[smer]);
    for (auto it = waypoints.begin(); it != waypoints.end(); it++) {
        int cx = GetX();
        int cy = GetY();
        int wx = (*it).x;
        int wy = (*it).y;
        if (cx == wx && cy == wy) smer = (*it).s;
    }    
    int cx = GetX();
    int cy = GetY();
    int tx = level1ciel.x;
    int ty = level1ciel.y;
    if (cx == tx && cy == ty) {
        gamescene->Escape();
        gamescene->delCreep(this);
    }
}

int CCreep::GetHP() {
    return hp;
}

void CCreep::Damage(int what) {
    hp -= what;
    if (hp <= 0) gamescene->delCreep(this);
}


